<?php

namespace App\ZhCN;

if (!defined('IN_PX'))
    exit;

use App\Service;
use App\Repository;
use App\Tools\Auxi;
use App\Tools\Html;
use App\Tools\Spell;
use Phoenix\Log\Log4p as logger;

/**
 * 首页
 */
class Index {

    private function __Controller() {}

    private function __Value($__PACKAGE__, $__ROOT__, $__RM__, $setting, $cfg, $__LANGUAGE_ID__) {}

    protected function __Inject($session, Service\Archives $servArc, Repository\Archives $repoArc,
                                Repository\Category $category) {}

    public function index() {
        if ($this->servArc->chkCategoryDataView()) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            $_model['aryAd'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 0);

            $_model['homeNews'] = $this->repoArc->findHomeNews(21, $this->__LANGUAGE_ID__, 0, 15);
            //首页底部链接
            return array (
                'model' => $_model,
                'view' => 'index/index'
            );
        }
        return 404;
    }

    public function category($__Route = array('/*/{aliasId}/{page:\d*}', '/category/{aliasId}/{page}')) {
        if (!is_null(($_model = $this->servArc->getCategoryRs($this->aliasId, true)))) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            $_model['aliasId'] = $this->repoArc->aliasId($this->__LANGUAGE_ID__, $this->aliasId);
            if (!isset($this->page)) {
                $this->page = 1;
                
            }
            $_model['currentPage'] = $this->page;
            //根目录下的总条数
            $_model['rootCategoryTotal'] = $this->servArc->findAllRootCount($_model['rootId']);

            if ($_model['categoryLevel'] == 1 && in_array($_model['channelType'], array(20))) { //列表
                $_model['currentCategoryTotal'] = $_model['rootCategoryTotal'];
                $_model['currentListRs'] = $this->servArc->findAllRoot($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            } else if ($_model['channelType'] == 1) {
                $user = $this->session->member;
                if (!$user) {
                    $user['member_id'] = 0;
                }
                $_model['currentListRs'] = $this->repoArc->findProduct($_model['categoryId'], $user['member_id'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            } else {
                $_model['currentListRs'] = $this->servArc->findAll($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            }
//            var_dump($_model['currentListRs']);die;

            return array(
                'model' => $_model,
                'view' => $this->setting['aryChannelTypeMapping'][$this->__PACKAGE__][$_model['channelType']][1]
            );
        }
        return 404;
    }

    public function detail($__Route = array('/{id:\d+}', '/product/detail/{id:\d+}', '/college/detail/{id:\d+}', '/news/detail/{id:\d+}', '/customer/detail/{id:\d+}')) {
        $_GET['aliasId'] = $this->__RM__;
        if (!is_null(($_model = $this->servArc->getSubstance($this->id)))) {
            $parent = explode('.', trim($_model['idTree'], '.'));
            $_model['parentId'] = $parent[1];
//            var_dump($_model['aliasId']);die;
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            $_model['substance'] = Html::outputToText($_model['substance']);

            if ($_model['channelType'] == 0) { //产品多图
                $_model['pics'] = $this->repoArc->productPics($this->id);
                $_model['link'] = $this->repoArc->productLink($this->id);
            }

            return array(
                'model' => $_model,
                'view' => $this->setting['aryChannelTypeMapping'][$this->__PACKAGE__][$_model['channelType']][3]
            );
        }
        return 404;
    }

    public function news($__Route = array('/news/{id}/{page}/{page2}')) {
        $this->aliasId = 'news';
        if (!is_null(($_model = $this->servArc->getCategoryRs($this->aliasId, true)))) {

            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            if (!isset($this->id)) {
                $this->id = 21;
            }
            if (!isset($this->page)) {
                $this->page = 1;
            }
            if (!isset($this->page2)) {
                $this->page2 = 1;
            }
            $_model['currentPage'] = $this->page;
            $_model['currentPage2'] = $this->page2;
            $_model['currentPageSize'] = 4;
            $_model['currentPageSize2'] = 3;
            $_model['aliasId'] = 'news/' . $this->id;
            $_model['aliasId2'] = 'news/' . $this->id . '/' . $_model['currentPage'];
            $_GET['page2'] = $this->page2;
            //根目录下的总条数
            $_model['currentCategoryTotal'] = $this->repoArc->findAllCount($this->id);
            $_model['currentCategoryTotal2'] = $this->repoArc->findAllCount(22);

            $_model['currentListRs'] = $this->repoArc->findAll($this->id,
                $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                $_model['currentPageSize']);

            $_model['currentListRs2'] = $this->repoArc->findAll(22,
                $_model['currentPageSize2'] * ($_model['currentPage2'] - 1),
                $_model['currentPageSize2']);
            $_model['id'] = $this->id;
            return array(
                'model' => $_model,
                'view' => 'news/index'
            );
        }
        return 404;
    }

    public function recruit($__Route = array('/contact')) {
        $_model['currentListRs'] = $this->repoArc->findRecruitAll('#@__@recruit');

        return array(
            'model' => $_model,
            'view' => 'contact/index'
        );
    }

    public function about($__Route = array('/about/index')) {
        $_model['currentListRs'] = $this->repoArc->findCertificate(0);

        $_model['currentListRs1'] = $this->repoArc->findCertificate(4);
        $_model['currentListRs2'] = $this->repoArc->findCertificate(5);

        $_model['aryBrand'] = $this->repoArc->getSynopsis($this->__LANGUAGE_ID__, 0);   // 品牌故事
        $_model['aryLi'] = $this->repoArc->getSynopsis($this->__LANGUAGE_ID__, 1);   // 我们的理念
        return array(
            'model' => $_model,
            'view' => 'about/index'
        );
    }

    public function brand($__Route = array('/brand/index')) {
        $_model['currentListRs'] = $this->repoArc->findCertificate(1);

        $_model['currentListRs1'] = $this->repoArc->findCertificate(6);
        $_model['currentListRs2'] = $this->repoArc->findCertificate(7);

        $_model['aryLarge'] = $this->repoArc->getSynopsis($this->__LANGUAGE_ID__, 5);   // 大骨熬汤
        $_model['aryVideo'] = $this->repoArc->getSynopsis($this->__LANGUAGE_ID__, 7);   // 加盟优势
        return array(
            'model' => $_model,
            'view' => 'brand/index'
        );
    }

    public function join($__Route = array('/join/index')) {
        $_model['currentListRs'] = $this->repoArc->findCertificate(2);

        $_model['currentListRs1'] = $this->repoArc->findCertificate(9);
        $_model['currentListRs2'] = $this->repoArc->findCertificate(10);

        $_model['aryLarge1'] = $this->repoArc->getSynopsis($this->__LANGUAGE_ID__, 2);   // 加盟优势
        $_model['aryLarge2'] = $this->repoArc->getSynopsis($this->__LANGUAGE_ID__, 3);   // 加盟优势
        $_model['aryLarge3'] = $this->repoArc->getSynopsis($this->__LANGUAGE_ID__, 4);   // 加盟优势
        $_model['aryLarge4'] = $this->repoArc->getSynopsis($this->__LANGUAGE_ID__, 6);   // 加盟优势
        return array(
            'model' => $_model,
            'view' => 'join/index'
        );
    }

    public function territory($__Route = array('/territory/index')) {
        $_model = array();
        $_model['map'] = $this->repoArc->findMap();

        return array(
            'model' => $_model,
            'view' => 'territory/index'
        );
    }


}
